﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");


using ConsoleApp1;
using ConsoleApp1.CSharpProgrammingGuide;
using ConsoleApp1.EventSample;
using ConsoleApp1.ExpressionTrees;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq.Expressions;
using System.Text;

//TransateExpressionTrees.Show();

/*Expression<Func<string, bool>> expr = name => name.Length > 10 && name.StartsWith("G");
Console.WriteLine(expr);

AndAlsoModifier treeModifier = new AndAlsoModifier();
Expression modifiedExpr = treeModifier.Modify((Expression)expr);
Console.WriteLine(modifiedExpr);*/



/*ExampleClass ec = new ExampleClass();
ec.exampleMethod1(1, 2);

dynamic dynamic_ec = new ExampleClass();
dynamic_ec.exampleMethod1(1, 2);

dynamic d = 1;
var testSum = d + 3;
Console.WriteLine(testSum);

dynamic d1 = 7;
dynamic d2 = "a string";
dynamic d3 = DateTime.Today;
dynamic d4 = Process.GetProcesses();

int i = d1;
string str = d2;
DateTime dt = d3;
Process[] proc = d4;

*/


//ProgrammingConcepts.Show();

/*double max = double.MaxValue;
double min = double.MinValue;
Console.WriteLine($"The range of double is {min} to {max}");

double third = 1.0 / 3.0;
Console.WriteLine(third);

decimal dMax = decimal.MaxValue;
decimal dMin = decimal.MinValue;
Console.WriteLine($"The range of decimal is {dMin} to {dMax}");

*/

//return 0;

/*if(args.Length == 0)
{
    Console.WriteLine("Please enter a numeric argument.");
    Console.WriteLine("Usage: Factorial <num> ");
    return 1;
}
// Try to convert the input arguments to numbers. This will throw an exception if the argument is not a number.
// num = int.Parse(args[0]);

int num;
bool test = int.TryParse(args[0], out num);
if (!test)
{
    Console.WriteLine("Please enter a numeric argument.");
    Console.WriteLine("Usage: Factorial <num> ");
    return 1;
}
// Calculate factorial.
long result = Functions.Factorial(num);
if(result == -1)
{
    Console.WriteLine("Input must be >= 0 and <= 20.");
}
else
{
    Console.WriteLine($"The Factorial of {num} is {result}");
}
return 0;*/

// 在顶层文件中可以出现类、命名空间等

/*MyClass.TestMethod();
MyNamespace.MyClass.MyMethod();

public class MyClass
{
    public static void TestMethod()
    {
        Console.WriteLine("Hello World!");
    }
}

namespace MyNamespace
{
    class MyClass
    {
        public static void MyMethod()
        {
            Console.WriteLine("Hello World from MyNamespace.MyClass.MyMethod!");
        }
    }
}
*/


/*//var p = new Person() { };
var p2 = new Person() { FirstName = "张", LastName = "三" };



// 声明必须的字段类型
class Person
{
    public required string LastName { get; set; }
    public required string FirstName { get; set; }
}

*/

//RecordExamples.Show();

//CSharpMethodExample.Show();

//EventApp.Show();

/*
//House mansion1 = new House { Name = "McMansion", Mortgage = 250000 };
//House mansion2 = mansion1.Clone();
//Console.ReadLine();
//public class Assert
//{
//    public string Name;
//    public virtual Assert Clone() => new Assert() { Name = Name };
//}

//public class House : Assert
//{
//    public decimal Mortgage;
//    public override House Clone()
//    {
//        return new House()
//        {
//            Mortgage = Mortgage,
//            Name = Name
//        };
//    }
//}
*/

/*using var c = new NamedPipeClientStream("pipedream");
c.Connect();
Console.WriteLine(c.ReadByte());
c.WriteByte(200);*/

Console.WriteLine("*********************");

using var client = new NamedPipeClientStream("pipedream");
client.Connect();
client.ReadMode = PipeTransmissionMode.Message;

Console.WriteLine(Encoding.UTF8.GetString(ReadMessage(client)));

byte[] msg = Encoding.UTF8.GetBytes("Hello right back!");
client.Write(msg, 0, msg.Length);
Console.ReadLine();

byte[] ReadMessage(PipeStream stream)
{
    MemoryStream ms = new MemoryStream();
    byte[] buffer = new byte[0x1000];
    do
    {
        ms.Write(buffer, 0, stream.Read(buffer, 0, buffer.Length));
    } while (!stream.IsMessageComplete);
    return ms.ToArray();
}

